package com.bwf.lgqingdan.gui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/20.
 */

public class NestRadioGroup extends LinearLayout{
    private List<RadioButton> radioButtons = new ArrayList<>();
    private RadioButton checkedRadioButton;
    private CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                for (int i = 0; i < radioButtons.size(); i++) {
                    if(buttonView != radioButtons.get(i) && radioButtons.get(i).isChecked()){
                        radioButtons.get(i).setChecked(false);
                    }
                }
                if(onCheckedChangeListener != null){
                    onCheckedChangeListener.onCheckedChanged(NestRadioGroup.this,buttonView.getId());
                }
            }
        }
    };

    public NestRadioGroup(Context context) {
        super(context);
    }

    public NestRadioGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    private boolean isInit;
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if(!isInit && getChildCount() > 0){
            radioButtons.clear();
            for (int i = 0; i < getChildCount(); i++) {
                fun(getChildAt(i));
            }
            isInit = true;
        }
    }

    private void fun(View view){
        if(view instanceof RadioButton){
            RadioButton rbtn = (RadioButton) view;
            rbtn.setOnCheckedChangeListener(checkedChangeListener);
            radioButtons.add(rbtn);
            if(checkedRadioButton == null){
                if(rbtn.isChecked()){
                    checkedRadioButton = rbtn;
                }
            }else{
                if(rbtn.isChecked()){
                    rbtn.setChecked(false);
                }
            }
            return;
        }
        if(view instanceof ViewGroup){
            ViewGroup viewGroup = (ViewGroup) view;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                fun(viewGroup.getChildAt(i));
            }
        }
    }

    private OnCheckedChangeListener onCheckedChangeListener;

    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }

    public void setChecked(int radioButtonId) {
        for (int i = 0; i < radioButtons.size(); i++) {
            if(radioButtons.get(i).getId() == radioButtonId){
                radioButtons.get(i).setChecked(true);
            }else{
                radioButtons.get(i).setChecked(false);
            }
        }
    }

    public interface OnCheckedChangeListener{
        void onCheckedChanged(NestRadioGroup nestRadioGroup,int checkedId);
    }
}
